﻿$(function () {
    //初始化列表信息
    init();


});

//初始化列表信息
function init(queryData) {
    $('#list').treegrid({
        title: '功能列表',
        idField: 'FunctionID',
        treeField: 'FunctionName',
        fitColumns: 'true',
        autoRowHeight: true,
        nowrap: true,
        striped: true,
        pagination: true,
        url: '/SystemFunction/GetList',
        columns: [[
			{ field: 'ck', checkbox: true , hidden: true },
			{ field: 'FunctionID', title: 'ID', hidden: true},
			{ field: 'FunctionParentID', title: '父级', width: 180, hidden: true},
			{ field: 'FunctionName', title: '功能名称', width: 180},
			{ field: 'FunctionUrl', title: '功能对应地址', width: 180},
			{ field: 'FunctionDescription', title: '功能描述', width: 180},
			{ field: 'CreateBy', title: '创建人', width: 180, hidden: true },
			{ field: 'CreateTime', title: '创建时间', width: 180, hidden: true },
			{ field: 'UpdateBy', title: '修改人', width: 180, hidden: true },
			{ field: 'UpdateTime', title: '修改时间', width: 180, hidden: true },
			{ field: 'Sort', title: '排序', width: 180, hidden: true },
		
        ]],
    });
}

//弹出增加窗口
function add() {
    var parentRows = $("#list").datagrid("getSelections");
    $('#dlg').dialog('open').dialog('setTitle', '增加');
    $('#fm').form('clear');
    var parentID = parentRows[0].FunctionID;
    $('#FunctionParentID').val(parentID);
    $('#comb_FunctionParentName').combotree('setValue', parentID);
    url = '/SystemFunction/Create';
    //if (parentRows.length == 1) {
    //    $('#dlg').dialog('open').dialog('setTitle', '增加');
    //    $('#fm').form('clear');
    //    var parentID = parentRows[0].FunctionID;
    //    $('#FunctionParentID').val(parentID);
    //    $('#comb_FunctionParentName').combotree('setValue', parentID);
    //    url = '/SystemFunction/Create';
    //}
    //else {
    //    $.messager.alert("友情提示", "请选择父级");
    //}
}

//弹出修改窗口
function edit() {
    $('#fm').form('clear');
    var rows = $('#list').treegrid('getSelections');
    if (rows.length == 1) {
        var updateID = rows[0].FunctionID;
        var myDate = new Date().getTime();//防止数据缓存
        $.getJSON("/SystemFunction/GetEntity?date=" + myDate, { ID: updateID }, function (data) {
            //绑定数据显示到用户控件上面
            $('#fm').form('load', data);
            $('#comb_FunctionParentName').combotree('setValue', data.FunctionParentID);
        })
        $('#dlg').dialog('open').dialog('setTitle', '修改');
        url = '/SystemFunction/Edit';
    }
    else {
        $.messager.alert("友情提示", "每次只能修改一行数据，你已经选择了<font color='red' size='6'>" + rows.length + "</font>行");
    }
}

//保存数据
function save() {
    $('#fm').form('submit', {
        url: url,
        onSubmit: function () {
            return $(this).form('validate');
        },
        success: function (result) {
            if (result == 'OK') {
                $('#dlg').dialog('close');      // close the dialog  
                $('#list').treegrid('reload');    // reload the user data  
            } else {
                $.messager.show({
                    title: 'Error',
                    msg: result
                });
            }
        }
    });
}

//删除
function del() {
    var rows = $('#list').datagrid('getSelected');
    if (rows) {
        $.messager.confirm(delDlgTitle, delDlgMsg, function (r) {
            if (r) {
                $.post('/SystemFunction/Delete', { id: rows.FunctionID },
                    function (result) {
                        if (result == 'OK') {
                            $("#list").datagrid('clearSelections');
                            $('#list').datagrid('reload');    // reload the user data  
                        } else {
                            $.messager.show({   // show error message  
                                title: 'Error',
                                msg: result
                            });
                        }
                    });
            }
        });
    }
}